<?php

/**
 * category actions.
 *
 * @package    sf_sandbox
 * @subpackage category
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class categoryActions extends autocategoryActions
{
    public function executeEdit()
    {
        parent::executeEdit();
        $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/category/filters');
        if( !$this->category->getParentId() )
        {
            if(isset($this->filters['parent_id']))
            {
                $this->category->setParentId($this->filters['parent_id']);
            }
        }
    }
    public function executeEnable()
    {
        $id = (int)$this->getRequestParameter('id');
        $obj = CategoryPeer::retrieveByPk($id);
        $this->forward404Unless($obj);

        $obj->setStatusId(MyConstants::STATUS_APPROVED);
        $obj->save();
        $this->redirect('category/list?id=' . $id);
    }
    public function executeDisable()
    {
        $id = (int)$this->getRequestParameter('id');
        $obj = CategoryPeer::retrieveByPk($id);
        $this->forward404Unless($obj);

        $obj->setStatusId(MyConstants::STATUS_DELETED);
        $obj->save();
        $this->redirect('category/list?id=' . $id);
    }
    public function executeMoveup()
    {
        /* @var $obj ListValues */
        $id = (int)$this->getRequestParameter('id', 0);
        $obj = CategoryPeer::retrieveByPk($id);
        $this->forward404Unless($obj);
        $c = new Criteria();
        $c->add(CategoryPeer::PARENT_ID, $obj->getParentId());
        APriority::moveUp($c, $id);
        $this->redirect('category/list?filters[parent_id]=' . $obj->getParentId() . '&filter=filter');
    }
    public function executeCildren()
    {
    	$id = $this->getRequestParameter('id', 0);
    	$this->redirect('category/list?filter=1&filters[parent_id]=' . $id);
    }

    public function executeDelete()
    {
    	if( $this->getRequest()->getMethod() == sfRequest::GET )
        {
        	$this->category = CategoryPeer::retrieveByPK($this->getRequestParameter('id'));
            $this->forward404Unless($this->category);
        }else 
        {
        	parent::executeDelete();
        }
    	
    }
    protected function updateCategoryFromRequest()
    {
        parent::updateCategoryFromRequest();
        $this->category->setPermissions(json_encode($this->getRequestParameter('category[permissions]')));
    }
	
    public function executeAddbranch()
    {
        $a_category = CategoryTree::get();
        debugvar($a_category);
        die('a_category...');
    }

    public function executeGetcat()
    {
        //$this->getUser()->setCulture('lt');
        $this->parentId = (int)$this->getRequestParameter('parent_id', 0);
        //debugvar($parentId);
        //debugvar('parentId...');
        $this->categories = Category::getList($this->parentId);

        //    	debugvar($this->categories);
    }

    protected function saveCategory($category)
    {
        $category->setName($this->getRequestParameter('category[name]'), $this->getUser()->getCulture());

        if( !$this->getRequestParameter('id') )
        {
            $category->setStatusId(MyConstants::OBJ_STATUS_INACTIVE);
        }
        $category->save();


        $fileName = $this->getRequest()->getFileName('category[photo]');
        $filePath = $this->getRequest()->getFilePath('category[photo]');
        if( !empty($filePath) )
        {
            Files::saveTo($fileName
            			, $filePath
            			, $category->getId()
            			, get_user_id()
            			, MyConstants::TYPE_CATEGORY
            			, $this->getUser()->getCulture()
            			, MyConstants::STATUS_APPROVED
            			, $fileName
            			, '');
        }

    }
     

    /**
     * temporary - from product
     *
     */
    public function executeIndex()
    {
        $this->forward('category', 'list');
//        $c = new Criteria();
//        $c->add(ProductPeer::STATUS_ID, MyConstants::OBJ_STATUS_ACTIVE);
//        $this->products = ProductPeer::doSelect($c);
    }

    /**
     * return category list
     *
     */
    public function executeListById()
    {
        //debugvar($id,'list-action');
        $this->categories = Category::getRealList($this->getRequestParameter('id'));
    }
	/**
	 * 
	 * delete category image
	 */
	public function executeDeleteimg()
    {
        if( $this->getRequestParameter('uname') )
        {
            $uname = Files::getNameFromUrl($this->getRequestParameter('uname'));
            //$file = Files::getNameFromUrl($this->getRequestParameter('uname'));
            Files::deleteByUname($uname, true);
        }

        $this->redirect('category/edit?id=' . $this->getRequestParameter('id'));
    }
    /**
     * return category list by parent id
     *
     */
    public function executeDetail()
    {
        //debugvar($this->getRequestParameter('id'),'detail-action');
        $this->arCategories = Category::getRealList($this->getRequestParameter('id'));
        $this->id = $this->getRequestParameter('id');
        $this->branch = $this->getRequestParameter('branch');
    }

    /**
     * return category list by parent id
     *
     */
    public function executeMenuTop()
    {
        //debugvar($this->getRequestParameter('id'),'detail-action');
        $this->arCategories = Category::getRealList($this->getRequestParameter('id'));
        $this->id = $this->getRequestParameter('id');
    }

    
    protected function getCategoryOrCreate($id = 'id')
    {
        /* @var $category Category */
        $category = parent::getCategoryOrCreate($id);
        $category->setCulture(sfContext::getInstance()->getUser()->getCulture());

        return $category;
    }


}
